﻿using Lemon.Blog.Base.Dtos;
using Lemon.Blog.Enum;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;

namespace Lemon.Blog.Base
{
    public class PublicMethodService:ApplicationService, IPublicMethodService
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly IConfiguration configuration;

        public PublicMethodService(IWebHostEnvironment webHostEnvironment,
            IConfiguration configuration)
        {
            this._webHostEnvironment = webHostEnvironment;
            this.configuration = configuration;
        }

        public async Task<string> UploadFileAsync(IFormFile file, [FromForm]UploadFileDto dto)
        {
            string filePath = _webHostEnvironment.WebRootPath;
            string serverUrl = configuration.GetSection("App")["SelfUrl"];
            string RelativeUri = "";
            switch (dto.FileType)
            {
                case UploadFileTypeEnum.HeadPhoto:
                    RelativeUri = "/img/HeadPhoto/temp-" + CurrentUser.UserName + file.FileName[file.FileName.LastIndexOf('.')..];
                    filePath += RelativeUri;
                    if (!Directory.Exists(filePath[0..filePath.LastIndexOf('/')]))
                    {
                        var directoryInfo = new DirectoryInfo(filePath[0..filePath.LastIndexOf('/')]);
                        directoryInfo.Create();
                    }
                    ResizeAndSaveImg(file, filePath);
                    break;
                case UploadFileTypeEnum.BlogPicture:
                    RelativeUri = "/img/BlogPicture/" + GuidGenerator.Create() + file.FileName[file.FileName.LastIndexOf('.')..];
                    filePath += RelativeUri;
                    if (!Directory.Exists(filePath[0..filePath.LastIndexOf('/')]))
                    {
                        var directoryInfo = new DirectoryInfo(filePath[0..filePath.LastIndexOf('/')]);
                        directoryInfo.Create();
                    }
                    await SaveImg(file, filePath);
                    break;
                default:
                    throw new UserFriendlyException("参数FileType错误");
            }
            return serverUrl + RelativeUri;
        }

        [NonAction]
        public static void ResizeAndSaveImg(IFormFile file,string filePath)
        {
            using var inputStream = new SKManagedStream(file.OpenReadStream());
            using var original = SKBitmap.Decode(inputStream);
            using var resized = original.Resize(new SKImageInfo(180, 180), SKFilterQuality.High);
            using var image = SKImage.FromBitmap(resized);
            var fileStream = new FileStream(filePath, FileMode.Create);
            image.Encode(SKEncodedImageFormat.Jpeg, 100).SaveTo(fileStream);
            fileStream.Flush();
            fileStream.Close();
        }

        [NonAction]
        public static async Task SaveImg(IFormFile file, string filePath)
        {
            using var stream = File.Create(filePath);
            await file.CopyToAsync(stream);
        }
    }
}